package com.redfootdev.horseskills;

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.util.Vector;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import com.redfootdev.horseskills.HorseMonitor;
import com.redfootdev.horseskills.HorseDatabase;
import com.redfootdev.horseskills.PlayerDatabase;



public class HorseSkills extends JavaPlugin implements Listener {
	
	public HorseDatabase hdb;
	public Trample trample;
	public PlayerDatabase pdb;
	public Strength strength;
	public Agility agility;
	public Attractiveness attractiveness;
	public Intelligence intelligence;

	
	public HorseSkills() {
		pdb = new PlayerDatabase((JavaPlugin) this);
		hdb = new HorseDatabase((JavaPlugin) this);
		trample = new Trample();
		strength = new Strength();
		agility = new Agility();
		intelligence = new Intelligence();
		attractiveness = new Attractiveness();
	}

	//Runs this on when the server starts up
    @Override
    public void onEnable(){
    	this.saveDefaultConfig();
    	getLogger().info(" -- Config loaded! -- ");
    	new HorseMonitor(this, hdb, pdb);
    	hdb.saveInterval(this.getConfig().getInt("SaveInterval"));
    	pdb.saveInterval(this.getConfig().getInt("SaveInterval"));
    	//TODO: Reestablish timing for players on horses.
    }
 
    //Runs this on server shutdown
    @Override
    public void onDisable() {
    	
    	//TODO: Need to suspend timing for horse riders.
    	
    	try {
			pdb.dump();
			hdb.dump();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	getLogger().info(" -- HorseSkills shutting down -- ");
    }
    
    
	public boolean onCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {
		Player player = (Player) sender;
		
		

		if (commandLabel.equalsIgnoreCase("entitypolice")
				|| commandLabel.equalsIgnoreCase("ep")) {
			player.sendMessage("dfgfhbgfhf");
			HorseCounter ec = new HorseCounter();
			String subCommand = args.length > 0 ? args[0].toLowerCase() : "";
			if (subCommand.equalsIgnoreCase("count")) {
				player.sendMessage("count is being executed");

				// /entitypolice count
				String entityName = args.length >= 2 ? args[1].toLowerCase(): "";
				String worldName = args.length == 3 ? args[2].toLowerCase(): "";
					if (args.length >= 2) {
						String count = ec.countEntity(player, entityName,
								worldName, "HorseSkills", args);
						player.sendMessage("Sending message \"" + count + "\"");
						player.sendMessage(count);
						return true;
					} else {
						return false;
					}
				 
    
			}
		}
			
		if(cmd.getName().equalsIgnoreCase("go")){
			if (!(sender instanceof Player)) {
				sender.sendMessage("This command can only be run by a player.");
			} else {
					
				Vector vec = player.getLocation().getDirection();
				vec= vec.multiply(5);
				Location loc = player.getLocation();
				LivingEntity horsey = (LivingEntity)player.getWorld().spawnEntity(loc.add(vec), EntityType.HORSE);
				horsey.setRemoveWhenFarAway(false);
				this.getLogger().info(player.getEntityId() +".");
					
					
			}
			
				// doSomething
			return true;
		}
		
		
		
		if(cmd.getName().equalsIgnoreCase("strength")){			
			
			if(player.getVehicle().getType() != EntityType.HORSE) {
				sender.sendMessage(ChatColor.AQUA + "You need to be on a horse to view it's Strength Stats!");
			} else {
			sender.sendMessage(" " + strength.toString(hdb, pdb, player.getVehicle(), player));
			return true;
			}
		}
		if(cmd.getName().equalsIgnoreCase("agility")){			
			if(player.getVehicle().getType() != EntityType.HORSE) {
				sender.sendMessage(ChatColor.AQUA + "You need to be on a horse to view it's Agility Stats!");
			} else  {
			sender.sendMessage(" " + agility.toString(hdb, pdb, (Horse)player.getVehicle(), player));
			return true;
			}
		}
		if(cmd.getName().equalsIgnoreCase("trample")){			
			if(player.getVehicle().getType() != EntityType.HORSE) {
				sender.sendMessage(ChatColor.AQUA + "You need to be on a horse to view it's Trample Stats!");
			} else {
			sender.sendMessage(" " + trample.toString(hdb, pdb, player.getVehicle(), player));
			return true;
			}
		}
		if(cmd.getName().equalsIgnoreCase("attractiveness")){			
			if(player.getVehicle().getType() != EntityType.HORSE) {
				sender.sendMessage(ChatColor.AQUA + "You need to be on a horse to view it's Attractiveness Stats!");
			} else  {
			sender.sendMessage(" " + attractiveness.toString(hdb, pdb, (Horse)player.getVehicle(), player));
			return true;
			}
		}
		if(cmd.getName().equalsIgnoreCase("intelligence")){			
			if(player.getVehicle().getType() != EntityType.HORSE) {
				sender.sendMessage(ChatColor.AQUA + "You need to be on a horse to view it's Intelligence Stats!");
			} else {
			sender.sendMessage(" " + intelligence.toString(hdb, pdb, player.getVehicle(), player));
			return true;
			}
		}
		if(cmd.getName().equalsIgnoreCase("tame")){
			List<Entity> stuff;
			stuff = player.getNearbyEntities(5, 5, 5);
			ListIterator<Entity> itr = stuff.listIterator();
			while (itr.hasNext()){
				Entity e = itr.next();
				if(e.getType() == EntityType.HORSE) {
					Horse horse = (Horse)e;
					if(!horse.isTamed()) {
						horse.setTamed(true);
					}
						
				}
			}
			return true;
		}
		
		if(cmd.getName().equalsIgnoreCase("dump")){			
			try {
				hdb.dump();
				pdb.dump();
			} catch (IOException e) {
				e.printStackTrace();
			}
			this.getLogger().info("dumped the metadata into the database.");
			return true;
		}
		return false;
	}
}
    